<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="margin-top: 16px;">
              <el-button type="primary" size="small" @click="isShowDialog = true">添加角色</el-button>
            </el-row>
            <!-- 角色表格 -->
            <el-table :data="roleList">
              <el-table-column prop="name" label="角色名称" />
              <el-table-column prop="description" label="角色描述" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="showEditDialog(row.id)">编辑</el-button>
                  <el-button type="text" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center">
              <el-pagination
                :total="total"
                :page-size="pageConfig.pagesize"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-row type="flex" style="margin-top: 16px;">
              <el-col :span="16">
                <el-form label-width="88px">
                  <el-form-item label="公司名称">
                    <el-input v-model="companyInfo.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="companyInfo.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="companyInfo.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="companyInfo.remarks" disabled type="textarea" />
                  </el-form-item>
                </el-form>
              </el-col>
              <el-col />
            </el-row>

          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
        <!-- form model 数据对象 rules 规则 -->
        <!-- form-item prop 字段名 -->
        <el-form ref="addRoleForm" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// api
import { getCompanyInfo, getRoleList, addRole, getRoleDetail, editRole, delRole } from '@/api/setting'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      companyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      roleList: [],
      isShowDialog: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 10, message: '名称在10个字符以内', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 100, message: '介绍在100个字符以内', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  async created() {
    this.companyInfo = await getCompanyInfo(this.companyId)
    this.loadPage()
  },
  methods: {
    currentChange(newPage) {
      // 将用户选的页码放入配置当中
      this.pageConfig.page = newPage
      this.loadPage()
    },
    async loadPage() {
      const { rows, total } = await getRoleList(this.pageConfig)
      this.roleList = rows
      this.total = total
    },
    async showEditDialog(id) {
      this.formData = await getRoleDetail(id)
      this.isShowDialog = true
    },
    async btnOK() {
      // 校验表单
      await this.$refs.addRoleForm.validate()
      // 发请求
      if (this.formData.id) {
        // 编辑
        await editRole(this.formData)
      } else {
        // 新增
        await addRole(this.formData)
      }
      // 关闭弹窗
      this.isShowDialog = false
      // 更新数据
      this.loadPage()
      // 提示用户
      this.$message.success('操作成功')
    },
    btnCancel() {
      // 清理数据清理报错
      this.formData = {
        name: '',
        description: ''
      }
      // 这个只能回复表单中带 prop 绑定的数据初始值,
      // 对于回显额外追加的 id 之类不会处理
      this.$refs.addRoleForm.resetFields()
      // 关弹窗
      this.isShowDialog = false
    },
    async delRole(id) {
      // 二次询问
      await this.$confirm('是否确认删除')
      // 发请求
      await delRole(id)
      // 提醒用户
      this.$message.success('删除成功')
      // 更新页面
      this.loadPage()
    }
  }
}
</script>

<style>

</style>
